package 用栈实现队列;

import java.util.Stack;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 用栈实现队列
 * @date 2022/10/23 14:57
 */
public class Demo232 {
    Stack<Integer> inStack;
    Stack<Integer> outStack;
    public Demo232() {
        inStack=new Stack();
        outStack=new Stack();

    }
    //void push(int x) 将元素 x 推到队列的末尾
    public void push(int x) {
        inStack.push(x);
    }
    //int pop() 从队列的开头移除并返回元素
    public int pop() {
        //如果出栈是空的话，则先将入栈的所有元素压入出栈中，如果不为空，则直接出栈
        if (outStack.empty()){
            while (!inStack.isEmpty()){
                outStack.push(inStack.pop());
            }
        }
        return outStack.pop();

    }
    //int peek() 返回队列开头的元素
    public int peek() {
        int pop = this.pop();
        outStack.push(pop);
        return outStack.peek();
    }
    //boolean empty() 如果队列为空，返回 true ；否则，返回 false
    public boolean empty() {
         return inStack.empty()&& outStack.empty();
    }
}
